import Weapon from "./weapon.js"
import LaserWeapon from "./laser_weapon.js"
import WaveWeapon from "./wave_weapon.js"
import {Drop, Empty, Wall} from "./wall.js"
import GameMap from "./game_map.js"
import Message from "./message.js"

class WeaponUtils {
    constructor(game) {
        this.game = game;
    }

    draw_weapon() {
        let game = this.game;
        let weapon_list = game.game_info.weapon_list;

        for (let i = 0; i < weapon_list.length; i++) {
            weapon_list[i].draw();
        }
    };

    add_weapon(weapon_type, row, col) {
        let game = this.game;
        if (game.spirit_utils.has_spirit_on_cell(row, col)) return;
        let map_cell = game.map.row_col_2_vector(row, col);
        let create_test = GameMap.allow_create_weapon(game, row, col);
        if (map_cell.role !== 1 || map_cell.is_start_end_cell() || !create_test) {
            let msg = {text: "此处禁止操作", size: 1, type: "warning"};
            // this.game.message_list.push(new Message(this.game, msg));
            Message.add_message(msg);
            return;
        }

        let weapon_list = game.game_info.weapon_list;
        let new_weapon = this.new_weapon(weapon_type);
        new_weapon.set_position(row, col);

        let gold = new_weapon.config.gold;
        if (game.game_info.gold < gold) {
            let msg = {text: "金币不足", size: 1, type: "warning"};
            // this.game.message_list.push(new Message(this.game, msg));
            Message.add_message(msg);
            return;
        }
        game.game_info.gold -= gold;
        new_weapon.map_cell.role = weapon_type;

        weapon_list.push(new_weapon);
        if (game.map.is_in_path(new_weapon.map_cell)) {
            game.map.calc_path();
        }
        return true;
    };

    new_weapon(weapon_index, row, col) {
        let weapon_type_list = [Drop, Empty, Wall, Weapon, LaserWeapon, WaveWeapon];
        if (weapon_index < 0 && weapon_index >= weapon_type_list.length) return null;

        let game = this.game;
        let config = this.game.config.weapon_type[weapon_index];
        let weapon_type = weapon_type_list[weapon_index];
        return new weapon_type(game, row || -1, col || -1, config);

    };

    find_weapon(row, col) {
        let game = this.game;
        let weapon_list = game.game_info.weapon_list;
        let index = weapon_list.findIndex(function (weapon) {
            return weapon.col === col && weapon.row === row;
        });

        if (index !== -1) {
            return weapon_list[index];
        }
        return null;
    };

    remove_weapon(row, col) {
        let game = this.game;
        let map_cell = game.map.row_col_2_vector(row, col);

        let weapon_list = game.game_info.weapon_list;
        let index = weapon_list.findIndex(function (weapon) {
            return weapon.col === col && weapon.row === row;
        });
        if (index !== -1) {
            weapon_list.splice(index, 1);
            map_cell.role = 1;
            if (GameMap.check_cell_in_path(game, row, col, 1)) {
                game.map.calc_path();
            }
        }
    };

    weapon_fire() {
        let game = this.game;
        let weapon_list = game.game_info.weapon_list;
        for (let i = 0; i < weapon_list.length; i++) {
            weapon_list[i].fire();
        }
    };

    bullet_check_hit() {
        let game = this.game;
        let weapon_list = game.game_info.weapon_list;
        for (let i = 0; i < weapon_list.length; i++) {
            weapon_list[i].bullet_check_hit();
        }
    };

    draw_bullet() {
        let game = this.game;
        let weapon_list = game.game_info.weapon_list;

        for (let i = 0; i < weapon_list.length; i++) {
            weapon_list[i].draw_bullet();
        }
    };

    bullet_move_on() {
        let weapon_list = this.game.game_info.weapon_list;

        for (let i = 0; i < weapon_list.length; i++) {
            weapon_list[i].bullet_move_on();
        }
    };

    cls_bullet() {
        let weapon_list = this.game.game_info.weapon_list;
        for (let i = 0; i < weapon_list.length; i++) {
            weapon_list[i].cls_bullet();
        }
    };
}

export default WeaponUtils;